home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 4 / The Arsenal Files 4 (Arsenal Computer).ISO / casm / au116-as.exe / IBMDOS / EXECUTE.CPP < prev    next >
C/C++ Source or Header  |  1994-12-05  |  3KB  |  164 lines

  1. #include "..\au.hpp"
  2. #include "..\spawno\spawno.h"
  3. #include <process.h>
  4.  
  5. /*********************************************************************/
  6. BYTE is_stdout(void)
  7. {
  8.     _AH = 0x44;
  9.     _BX = 1;
  10.     _AL = 0;
  11.     __int__(0x21);
  12.     if (_DX & 0x01 && _DX & 0x80 && _DX & 0x40 && !(_DX & 0x04))
  13.         return TRUE;
  14.     else
  15.         return FALSE;
  16. }
  17.  
  18. /*********************************************************************/
  19. void far interrupt newfunc(...)
  20. {
  21.     return;
  22. }
  23. /********************************************************************/
  24. static enough_memory(AU *au, int k_needed)
  25. {
  26.     long memCurrent = farcoreleft();
  27.     if (memCurrent > (long)k_needed * 1024)
  28.         return 0;         /* Enough so regular spawn can be used */
  29.     else if (memCurrent > (long)k_needed * 1024 - IMAGE_MEMORY - au->memStart)
  30.         return 1;         /* Enough so spawno can still work */
  31.     else
  32.         return 2;         /* Not enough memory period */
  33. }
  34. /********************************************************************/
  35. int execute(AU *au, char *command, char *output, char *input, int k_needed)
  36. {
  37.     char **args = new char *[15];
  38.     char c;
  39.     int  ret;
  40.     int  i=0;
  41.     int  pos=0;
  42.     int  old_stdout;
  43.     int  old_stdin;
  44.     int  show;
  45.     int  temp;
  46.     void (far interrupt *inter)(...);
  47.  
  48.     if (au->show_execute)
  49.     {
  50.         au_printf_c(au, 13, "%s", command);
  51.         if (output != NULL && output[0] != '\0')
  52.             au_printf_c(au, 13, ">> %s", output);
  53.         if (input != NULL && input[0] != '\0')
  54.             au_printf_c(au, 13, "< %s", input);
  55.         au_printf(au, "\n");
  56.     }
  57.  
  58.     for (EVER)
  59.     {
  60.         ltrim(command+pos);
  61.         args[i++] = command+pos;
  62.         for (EVER)
  63.         {
  64.             c = command[pos];
  65.             if (c == ' ')
  66.             {
  67.                 command[pos] = '\0';
  68.                 pos++;
  69.                 break;
  70.             }
  71.             if (c == '\0')
  72.                 goto out_loop;
  73.             pos++;
  74.         }
  75.     }
  76.  out_loop:
  77.     args[i] = NULL;
  78.  
  79.     if (output != NULL && output[0] != '\0')
  80.         old_stdout = dup(1);       // duplicate handle of stdout
  81.  
  82.     if (input != NULL && input[0] != '\0')
  83.         old_stdin = dup(0);        // duplicate handle of stdin
  84.  
  85.     if (output != NULL && output[0] != '\0')
  86.     {
  87.         close(1);                   // close stdout
  88.         open(output, O_RDWR);       // open output as stdout
  89.     }
  90.     if (input != NULL && input[0] != '\0')
  91.     {
  92.         close(0);                   // close stdin
  93.         open(input, O_RDWR);       // open input as stdin
  94.     }
  95.  
  96.  
  97. #if 0
  98.      _AH = 0x44;
  99.     _BX = 1;
  100.     _AL = 0;
  101.     __int__(0x21);
  102.     temp = _DX;
  103.     if (temp & 0x02)
  104.         show = TRUE;
  105.     else
  106.         show = FALSE;
  107. #endif
  108.  
  109.  
  110. #if 0
  111.     inter = getvect(0x10);          // save old
  112.     if (!show)
  113.         setvect(0x10, newfunc);
  114. #endif
  115.  
  116.     switch (enough_memory(au, k_needed))
  117.     {
  118.     case 0:
  119.         ret = spawnvp(P_WAIT, args[0], args);
  120.         break;
  121.     case 1:
  122.         au_printf_c(au, 15, "Swapping AU\n");
  123.         ret = spawnvpo(".", args[0], (const char **)args);
  124.         break;
  125.     case 2:
  126.         au_printf_error(au, "Insufficient memory to run: %s", command);
  127.         exit(CANT_EXECUTE);
  128.     }
  129.  
  130. #if 0
  131.     setvect(0x10, inter);          // restore old
  132. #endif
  133.  
  134.     if (output != NULL && output[0] != '\0')
  135.     {
  136.         close(1);                   // reset old stdout
  137.         dup(old_stdout);
  138.         close(old_stdout);
  139.     }
  140.     if (input != NULL && input[0] != '\0')
  141.     {
  142.         close(0);                   // reset old stdin
  143.         dup(old_stdin);
  144.         close(old_stdin);
  145.     }
  146.  
  147.     if (ret < 0)
  148.     {
  149.         au_printf_error(au, "Unable to Execute '%s'", command);
  150.         exit(CANT_EXECUTE);
  151.     }
  152.     delete [] args;
  153.     return ret;
  154. }
  155. /********************************************************************/
  156. int execute_raw(AU *au, char *command)
  157. {
  158.     if (au->show_execute)
  159.         au_printf_c(au, 13, "%s", command);
  160.  
  161.     system(command);
  162.     return 0;
  163. }
  164.